﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SmartProject.Entity;
using SmartProject.Data;

using NHibernate;

namespace SmartProject
{
    public abstract class BusinessRulePreference
    {
        private string _preferenceName;
        private BusinessRule[] _businessRules;

        protected void Initialize(string preferenceName, BusinessRule[] businessRules)
        {
            _preferenceName = preferenceName;
            _businessRules = businessRules;
        }

        /// <summary>
        /// Gets the name of this preference.
        /// </summary>
        public string Name
        {
            get { return _preferenceName; }
        }

        /// <summary>
        /// If you have multiple business rules to choose from, this method
        /// will pick the business rule that you have already selected during
        /// your initial setup.
        /// </summary>
        /// <param name="session">Session used in this request.</param>
        /// <returns>Null if no preferred BusinessRule can be found.</returns>
        public BusinessRule GetSelectedBusinessRule(ISession session)
        {
            Preference preference = Preference.GetPreference(session, _preferenceName);
            if (preference != null)
            {
                string value = preference.PrefValue;
                foreach (BusinessRule businessRule in _businessRules)
                {
                    if (businessRule.Name == value)
                    {
                        return businessRule;
                    }
                }
            }
            Log.Error(session, this, "GetSelectedBusinessRule", 
                String.Format("Business Rule {0} is missing", _preferenceName));
            return null;
        }

        /// <summary>
        /// Gets selected business rule.
        /// </summary>
        /// <param name="session">Session used in this request.</param>
        /// <param name="name">Preference name.</param>
        public void SetSelectedBusinessRule(ISession session, string name)
        {
            Preference preference = Preference.GetPreference(session, _preferenceName);
            if (preference == null)
            {
                preference = new Preference();
                preference.PrefName = _preferenceName;
            }

            for (int i = 0; i < _businessRules.Length; i++)
            {
                if (_businessRules[i].Name == name)
                {
                    preference.PrefValue = name;
                    preference.Save(session);
                    return;
                }
            }

            throw new InvalidOperationException(String.Format("Invalid business rule name: {0}.", name));
        }

        /// <summary>
        /// Access the business rules stored by the preference type.
        /// </summary>
        public BusinessRule[] BusinessRules
        {
            get { return _businessRules; }
        }

        /// <summary>
        /// Gets whether this preference is used ONLY in initial setup. Some
        /// preferences may be configured after the initial setup/ install.
        /// </summary>
        public abstract bool IsInitialSetupOnly { get; }
    }
}
